1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.collect.testing.testers;
18
19 import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
20
21 import com.google.common.annotations.GwtCompatible;
22 import com.google.common.collect.testing.Helpers;
23 import com.google.common.collect.testing.MinimalSet;
24 import com.google.common.collect.testing.features.CollectionFeature;
25 import com.google.common.collect.testing.features.CollectionSize;
26
27 import java.util.Collection;
28 import java.util.Set;
29
30
31
32
33
34
35 @GwtCompatible
36 public class SetEqualsTester<E> extends AbstractSetTester<E> {
37 public void testEquals_otherSetWithSameElements() {
38 assertTrue(
39 "A Set should equal any other Set containing the same elements.",
40 getSet().equals(MinimalSet.from(getSampleElements())));
41 }
42
43 @CollectionSize.Require(absent = CollectionSize.ZERO)
44 public void testEquals_otherSetWithDifferentElements() {
45 Collection<E> elements = getSampleElements(getNumElements() - 1);
46 elements.add(getSubjectGenerator().samples().e3);
47
48 assertFalse(
49 "A Set should not equal another Set containing different elements.",
50 getSet().equals(MinimalSet.from(elements))
51 );
52 }
53
54 @CollectionSize.Require(absent = CollectionSize.ZERO)
55 @CollectionFeature.Require(ALLOWS_NULL_VALUES)
56 public void testEquals_containingNull() {
57 Collection<E> elements = getSampleElements(getNumElements() - 1);
58 elements.add(null);
59
60 collection = getSubjectGenerator().create(elements.toArray());
61 assertTrue("A Set should equal any other Set containing the same elements,"
62 + " even if some elements are null.",
63 getSet().equals(MinimalSet.from(elements)));
64 }
65
66 @CollectionSize.Require(absent = CollectionSize.ZERO)
67 public void testEquals_otherContainsNull() {
68 Collection<E> elements = getSampleElements(getNumElements() - 1);
69 elements.add(null);
70 Set<E> other = MinimalSet.from(elements);
71
72 assertFalse(
73 "Two Sets should not be equal if exactly one of them contains null.",
74 getSet().equals(other));
75 }
76
77 @CollectionSize.Require(absent = CollectionSize.ZERO)
78 public void testEquals_smallerSet() {
79 Collection<E> fewerElements = getSampleElements(getNumElements() - 1);
80 assertFalse("Sets of different sizes should not be equal.",
81 getSet().equals(MinimalSet.from(fewerElements)));
82 }
83
84 public void testEquals_largerSet() {
85 Collection<E> moreElements = getSampleElements(getNumElements() + 1);
86 assertFalse("Sets of different sizes should not be equal.",
87 getSet().equals(MinimalSet.from(moreElements)));
88 }
89
90 public void testEquals_list() {
91 assertFalse("A List should never equal a Set.",
92 getSet().equals(Helpers.copyToList(getSet())));
93 }
94 }